<script setup lang="ts">
  import { ref, watch } from 'vue';
  import { FormInstance, Message } from '@arco-design/web-vue';
  import useLoading from '@/hooks/loading';
  import { addcabinetType } from '@/api/archives';

  const formRef = ref<FormInstance | null>(null);
  const props = defineProps({
    visible: Boolean,
    params: Object,
    formType: {
      type: Array,
      default: () => [
        {
          name: 'type_name',
          label: '类型名称',
          type: 'a-input',
          fieldProps: {
            span: 24,
          },
          rules: [
            {
              required: true,
              message: '请输入',
            },
          ],
        },
        {
          name: 'type_number',
          label: '类型编号',
          type: 'a-input',
          fieldProps: {
            span: 24,
          },
          rules: [
            {
              required: true,
              message: '请输入',
            },
          ],
        },
      ],
    },
  });
  const { loading, setLoading } = useLoading();
  const isShow = ref(true);
  defineOptions({
    name: 'userAddSetting',
  });

  const formData = ref({
    type_name: '',
  });
  const emit = defineEmits(['update:visible', 'refresh']);

  const handleSubmit = async () => {
    if (!(await formRef.value?.checkForm())) {
      try {
        setLoading(true);
        const res = await addcabinetType(formRef.value?.formModel);
        Message.success(res.msg);
        emit('update:visible', false);
        setLoading(false);
        emit('refresh');
      } catch (error) {
        setLoading(false);
      }
    } else {
      Message.error('请正确填写表单！');
    }
  };
</script>

<template>
  <a-modal
    title="新增档案柜类型"
    :visible="visible"
    @ok="handleSubmit"
    title-align="start"
    :ok-loading="loading"
    @cancel="
      () => {
        emit('update:visible', false);
      }
    "
  >
    <pro-form
      v-if="isShow"
      ref="formRef"
      hiddenBtn
      :form-json="formType"
      :form-data="formData"
      @formModelChange="handleFormModelChange"
    ></pro-form>
  </a-modal>
</template>

<style scoped lang="less">
  :deep(.arco-select-view-inner) {
    text-align: left !important;
  }
</style>
